package learn.jooq;

import static learn.jooq.dao.Tables.KS_ORDER;

import java.sql.Connection;
import java.sql.DriverManager;
import java.time.LocalDateTime;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Record2;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.SelectField;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.jooq.impl.DefaultTransactionListener;

import learn.jooq.dao.tables.records.KsOrderRecord;

/**
 * @author suzheng
 * @date 2021-05-13
 **/
public class JooqMain {
    public static void main(String[] args) {
    }

    private static void usePool() {
        String userName = "ks";
        String password = "1QEF4prCouIqvUbW";
        String url = "jdbc:mysql://rm-wz9lv65k80o43w65s0o.mysql.rds.aliyuncs.com:3306/sibu_ks_live_mall";
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(url);
        basicDataSource.setUsername(userName);
        basicDataSource.setPassword(password);
        DefaultConfiguration defaultConfiguration = new DefaultConfiguration();
        defaultConfiguration.setSQLDialect(SQLDialect.MYSQL);
        DataSourceConnectionProvider dataSourceConnectionProvider  =new DataSourceConnectionProvider(basicDataSource);
        defaultConfiguration.setConnectionProvider(dataSourceConnectionProvider);
        defaultConfiguration.setTransactionListener(new DefaultTransactionListener());
        DSLContext create = new DefaultDSLContext(defaultConfiguration);

        Result<Record2<Long, LocalDateTime>> result = create.select(KS_ORDER.OID,KS_ORDER.CREATE_DT).from(KS_ORDER)
                .limit(5).fetch();
        for (Record r : result) {
            Long id = r.get(0,Long.class);
            LocalDateTime localDateTime = r.get(1,LocalDateTime.class);
            System.out.println(id + "---" + localDateTime.toString());
        }
    }

    private static void useJdbc() {
        String userName = "ks";
        String password = "1QEF4prCouIqvUbW";
        String url = "jdbc:mysql://rm-wz9lv65k80o43w65s0o.mysql.rds.aliyuncs.com:3306/sibu_ks_live_mall";

        // Connection is the only JDBC resource that we need
        // PreparedStatement and ResultSet are handled by jOOQ, internally
        try (Connection conn = DriverManager.getConnection(url, userName, password)) {
            DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
            Result<Record> result = create.select().from(KS_ORDER)
                    .limit(100).fetch();

            for (Record r : result) {
                Long id = r.getValue(KS_ORDER.OID);
                LocalDateTime localDateTime = r.getValue(KS_ORDER.CREATE_DT);
                System.out.println(id + "---" + localDateTime.toString());
            }
        }

        // For the sake of this tutorial, let's keep exception handling simple
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
